/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: TimeoutKeeper.java 658 2007-06-30 08:59:11Z jack.rybicki $
 */
package gammu.java.machines;

/**
 * 
 * Keeps execution of commands within specified time constraints.
 *
 */
class TimeoutKeeper {
	private volatile boolean running = false;
	private final long timeout;
	private final Thread target;

	public TimeoutKeeper(long timeout) {
		this.timeout=timeout;
		target = Thread.currentThread();
	}
	
	public TimeoutKeeper(Thread thread, long timeout) {
		this.timeout=timeout;
		this.target=thread;
	}
	
	public void start() {
		running = true; 
		(new Thread() {
			public void run() {
				try {
					sleep(timeout);
				} catch (InterruptedException e) {

				}
				if (running) {
					//ct.stop(new IOException("Timeout"));
					target.interrupt();
				}
			}
		}).start();
	}

	public void stop() {
		running = false;
	}
}